package object2;

/**
 * 封装练习
 */
public class EncapsulationExercise {

    public static void main(String[] args) {

        /*
            创建程序，在其中定义类：Account
            1)Account类要求具有属性：姓名(长度为2位3位或4位)、余额(>20)、密码(必须是六位)，如果不满足则给出提示信息
                并给出默认值
            2)通过setXxx的方法给Account的属性赋值
            3)测试
         */

        Account account = new Account();
        account.setName("jack");
        account.setBalance(60);
        account.setPassword("123456");
        account.showInfo();
    }
}

class Account {

    String name;

    double balance;

    String password;

    public Account() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        //长度校验
        if (name.length() < 2 || name.length() > 4) {
            System.out.println("Name must be between 2 and 4 characters");
        } else {
            this.name = name;
        }
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        //长度校验
        if (password.length() != 6) {
            System.out.println("Password must be 6 characters");
            this.password = "000000";
        } else {
            this.password = password;
        }
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public void showInfo() {
        //可以增加权限的校验
        System.out.println("账号信息name=" + name + "余额=" + balance + "密码=" + password);
    }
}
